package com.byedu.course.controller;

import com.alibaba.fastjson.JSON;
import com.byedu.course.WeiXinUserService;
import com.byedu.domain.course.WeiXinUserDO;
import com.byedu.domain.course.vo.ReturnWeiXinUserVO;
import com.byedu.domain.course.vo.WeiXinUserVO;
import com.byedu.util.yunpian.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登陆页面对应接口
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private static final Log LOGGER = LogFactory.getLog(LoginController.class);
    @Autowired
    private WeiXinUserService weiXinUserService;
    private static Logger logger = Logger.getLogger(LoginController.class);

    /**
     * 用户用微信账号登录
     *
     * @param weiXinUserDO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUser", method = {RequestMethod.POST})
    public ReturnWeiXinUserVO getUser(@RequestBody WeiXinUserDO weiXinUserDO) {
//        logger.warn("weiXinUserDO"+weiXinUserDO.toString());
        WeiXinUserVO weiXinUserVO = weiXinUserService.getWeiXinUserVOByOpenId(weiXinUserDO.getOpenId());
        if (weiXinUserVO == null) {
//            response.sendRedirect(CommonUrls.WeixinLoginUrl);
            return new ReturnWeiXinUserVO("user");
        } else {
            if (StringUtil.isNullOrEmpty(weiXinUserVO.getUserId())) {
                return new ReturnWeiXinUserVO("userid", weiXinUserVO);
            }
            if (StringUtil.isNullOrEmpty(weiXinUserVO.getCellPhone())) {
                return new ReturnWeiXinUserVO("phone", weiXinUserVO);
            }
            return new ReturnWeiXinUserVO(weiXinUserVO);
        }
    }

    /**
     * 用户用微信账号登录
     *
     * @param weiXinUserDO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/wx", method = {RequestMethod.POST})
    public WeiXinUserDO loginByWeixin(@RequestBody WeiXinUserDO weiXinUserDO) {
        LOGGER.info(JSON.toJSONString(weiXinUserDO));
        return weiXinUserService.loginByWeixin(weiXinUserDO);
    }

    /**
     * 发送验证码
     *
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/diss/sendcode")
    public String sendcode(@RequestParam(value = "userId", required = true) String userId,
                           @RequestParam(value = "phone", required = true) String phone) throws IOException {

//        String userCodeStr = YunPianUtil.getRandNum(1000, 9999) + "";
//        String text = "【贝易网校】您的验证码是" + userCodeStr;
//
//
//        YunPianUtil.sendSms(YunPianUtil.yunpan_apikey, text, phone);
//
//        int returnStr = weiXinUserService.updateWeiXinUserbyUserId(userId, userCodeStr, null);
//        if (returnStr > 0) {
//            return "验证码发送成功,请等待";
//        }
        logger.info("have no code!");
        return "验证码发送失败,请重试";
    }

    @ResponseBody
    @RequestMapping(value = "/submitphone")
    public ReturnWeiXinUserVO submitphone(@RequestParam(value = "userId", required = true) String userId,
                                          @RequestParam(value = "openId", required = true) String openId,
                                          @RequestParam(value = "code", required = true) String code,
                                          @RequestParam(value = "phone", required = true) String phone,
                                          HttpServletResponse response) throws IOException {
        WeiXinUserVO weiXinUserVO = weiXinUserService.getWeiXinUserVOByOpenId(openId);

        if (weiXinUserVO == null) {
//            response.sendRedirect(CommonUrls.WeixinLoginUrl);
            return new ReturnWeiXinUserVO("用户信息为空，请退出重新登录");
        } else {
            if (code.equals(weiXinUserVO.getIdentifyCode())) {
                int returnStr = weiXinUserService.updateWeiXinUserbyUserId(userId, code, phone);
                if (returnStr > 0) {
                    weiXinUserVO.setCellPhone(phone);
                    return new ReturnWeiXinUserVO(weiXinUserVO);
                } else {
                    return new ReturnWeiXinUserVO("验证码或手机号匹配不正确", weiXinUserVO);
                }
            } else {
                return new ReturnWeiXinUserVO("验证码或手机号匹配不正确", weiXinUserVO);
            }
        }
    }


//    @RequestMapping(value = "/tologin")
//    public void tologin(@RequestParam(value = "userId", required = false) String userId, HttpServletResponse response) {
//        try {
//            if (userId == null) {
//                response.sendRedirect(CommonUrls.WeixinLoginUrl);
//
//            } else {
//                WeiXinUserVO weiXinUserVO = weiXinUserService.getWeiXinUserVOByUserId(userId);
//                if (weiXinUserVO == null) {
//                    response.sendRedirect(CommonUrls.WeixinLoginUrl);
//                } else {
//                    response.sendRedirect(sendRedirect(weiXinUserVO));
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
